#!/usr/bin/env python
# -*- coding: utf-8 -*-
import gtk
import tilewidgets


class Input(object):
    """A form generic input;

    To create a new input you should put the apropriated wigdet on the
    subclass instance widget property.
    """
    no_label = False

    def __init__(self, name, label=None):
        """Create a input with the given label.
	"""
        self.name = name
        self.label = label or name
        self.widget = gtk.Entry()

    def get_value(self):
        """Return the input current value.
        """
        return self.widget.get_text().strip()

    @classmethod
    def get_for_type(self, type):
        """Get the input class that handle the given type.
        """


class SizeInput(Input):
    """A input to get a size in the form (width, height)
    """

    def __init__(self, name, label=None):
        """Create the size input.
	"""
	super(SizeInput, self).__init__(name, label)
        hbox = gtk.HBox()
        self.width_entry = gtk.Entry()
        self.height_entry = gtk.Entry()

        hbox.pack_start(self.width_entry, True)
        hbox.pack_start(gtk.Label(" x "), False)
        hbox.pack_start(self.height_entry, True)

        self.widget = hbox

    def get_value(self):
        """Get a value tuple
        """
        return (int(self.width_entry.get_text()),
                int(self.height_entry.get_text()))


class TileInput(Input):
    """A form input for getting tile indexes.
    """
    no_label = True

    def __init__(self, name, tileset):
        """
	"""
	super(TileInput, self).__init__(name)
        self.widget = tilewidgets.TileSetPallete(tileset)

    def get_value(self):
        """
        """
        return self.widget.get_selected_index()


class ObjectListInput(Input):
    """Input to select from a list of objects.
    """

    def __init__(self, name, objects, label=None):
        """A list of objects.
	"""
        super(ObjectListInput, self).__init__(name, label)
        self.objects = {}
        self.widget = gtk.combo_box_new_text()

        for obj in objects:
            self.objects[str(obj)] = obj
            self.widget.append_text(str(obj))

        self.widget.set_active(0)

    def get_value(self):
        """
        """
        return self.objects[self.widget.get_active_text()]


class Form(gtk.Table):
    """A sequence of inputs.
    """

    def __init__(self, inputs):
        """Build the form from the input list.
	"""
        super(Form, self).__init__()

        self.inputs = []
        for input in inputs:
            self.add_input(input)

    def add_input(self, input):
        """Adds a input to the form.
        """
        self.inputs.append(input)
        row = self.props.n_rows
        if input.no_label:
            self.attach(input.widget, 0, 2, row, row+1)
        else:
            self.attach(gtk.Label(input.label), 0, 1, row, row+1)
            self.attach(input.widget, 1, 2, row, row+1)

    def get_values(self):
        """Return a tuple with the ordererd values of the form inputs.
        """
        return tuple(input.get_value() for input in self.inputs)

if __name__ == '__main__':
    import tileset

    inputs = [
        Input("name"),
        SizeInput("size"),
        # TileInput("tileset", tileset.from_image("test", "hills.png", 32)),
        ObjectListInput("objects", [[1,2,3], (2,3,4), object(), type]),
        ]

    win = gtk.Window()

    win.add(Form(inputs))
    win.show_all()

    win.connect("delete-event", gtk.main_quit)

    gtk.main()
